import 'package:awesome_dialog/awesome_dialog.dart';
import 'package:flutter/material.dart';
import 'package:flutter_reader/dao/pay_data_manager.dart';
import 'package:flutter_reader/pages/Login/login_nav_page.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'package:flutter_reader/tools/globals.dart' as globals;
import 'package:tobias/tobias.dart' as tobias;
import 'package:fluwx/fluwx.dart' as fluwx;

class VipPage extends StatelessWidget {
  VipPage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    String code = 'month';


    // set up the list options
    Widget optionOne = SimpleDialogOption(
      child: Row(
        children: <Widget>[
          Image(
            width: ScreenUtil().setWidth(70),
            image: AssetImage('images/fl_AliPay.png'),
          ),
          Container(
            margin: EdgeInsets.only(left: ScreenUtil().setWidth(20)),
            child: Text('支付宝支付'),
          )
        ],
      ),
      onPressed: () {
        ///支付宝支付
        PayDao.confirmAliPay(globals.fullSessionID, code).then((value){
          print(value['data']);
          pay(value['data']);
        });
      },
    );
    Widget optionTwo = SimpleDialogOption(
      child: Row(
        children: <Widget>[
          Image(
            width: ScreenUtil().setWidth(70),
            image: AssetImage('images/fl_WeChat.png'),
          ),
          Container(
            margin: EdgeInsets.only(left: ScreenUtil().setWidth(20)),
            child: Text('微信支付'),
          )
        ],
      ),
      onPressed: () {
        ///微信支付
        PayDao.confirmWechatPay(globals.fullSessionID, code, globals.dateError).then((value) async{
          print(value);
          var isinstall = await fluwx.isWeChatInstalled();
          print('is installed = $isinstall');
          fluwx.payWithWeChat(
              appId: value['data']['appid'].toString(),
              partnerId: value['data']['partnerid'].toString(),
              prepayId: value['data']['prepayid'].toString(),
              packageValue: value['data']['package'].toString(),
              nonceStr: value['data']['noncestr'].toString(),
              timeStamp: int.parse(value['data']['timestamp']),
              sign: value['data']['sign'].toString()
          ).then((data) {
            print('data:$data');
          });
          print('支付结束');
          fluwx.responseFromPayment.listen((response){
            print('errorCode:${response.errCode}');
            print('${response.errStr}');
          });
        });
      },
    );

    SimpleDialog dialog = SimpleDialog(
      title: const Text('请选择支付方式'),
      children: <Widget>[
        optionOne,
        optionTwo,
      ],
    );

    // TODO: implement build
    return Scaffold(
      body: Container(
        child: Stack(
          children: <Widget>[
            Container(
              width: ScreenUtil().setWidth(1125),
              height: ScreenUtil().setHeight(2436),
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  colors: <Color>[
                    Color(0xff1F1B16),
                    Color(0xff3E372A)
                  ]
                )
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: ScreenUtil().setHeight(0)),
              width: ScreenUtil().setWidth(1125),
              child: Image(
                fit: BoxFit.fill,
                image: AssetImage('images/fl_vipvip.png'),
              ),
            ),
            Container(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  InkWell(
                    onTap: (){
                      Navigator.pop(context);
                    },
                    child:Container(
                      margin: EdgeInsets.only(top: ScreenUtil().setHeight(100),left: ScreenUtil().setWidth(40)),
                      height: ScreenUtil().setHeight(100),
                      child: Icon(
                        Icons.chevron_left,
                        color: Colors.white,
                        size: 50,
                      ),
                    ),
                  ),
                  Container(
                    width: ScreenUtil().setWidth(875),
                    margin: EdgeInsets.only(left: ScreenUtil().setWidth(125),top: ScreenUtil().setHeight(600)),
                    decoration: BoxDecoration(
                      color: Colors.transparent,
                      border: Border.all(
                        width: 1,
                        color: Colors.white
                      )
                    ),
                    child: Column(
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.only(top: ScreenUtil().setHeight(40)),
                          child: Text('VIP期限内 全站所有资源免费使用',
                            style: TextStyle(
                                color: Colors.white,
                                fontWeight: FontWeight.w500,
                                fontSize: ScreenUtil().setSp(50)
                            ),),
                        ),
                        Container(
                          margin: EdgeInsets.only(top: ScreenUtil().setHeight(40)),
                          child: Text('尊享VIP转区特权',
                            style: TextStyle(
                                color: Colors.white,
                                fontSize: ScreenUtil().setSp(50),
                              fontWeight: FontWeight.w500
                            ),),
                        ),
                        Container(
                          margin: EdgeInsets.only(top: ScreenUtil().setHeight(40),bottom: ScreenUtil().setHeight(40)),
                          child: Text('加V标识 独享尊贵身份',
                            style: TextStyle(
                                color: Colors.white,
                                fontWeight: FontWeight.w500,
                                fontSize: ScreenUtil().setSp(50)
                            ),),
                        )
                      ],
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: ScreenUtil().setHeight(200),left: ScreenUtil().setWidth(110)),
                    child: InkWell(
                      onTap: () async{
                        SharedPreferences shared = await SharedPreferences.getInstance();
                        if(shared.getString("login") == "true") {
                          ///开通会员
                          showDialog(
                            context: context,
                            builder: (BuildContext context) {
                              code = 'month';
                              return dialog;
                            },
                          );
                        }
                        else
                          {
                            print("先登录");
                            Navigator.push(context, MaterialPageRoute(
                              builder: (context) => LoginNavPage()
                            ));
                          }
                      },
                      child: InkWell(
                        child: Container(
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.all(Radius.circular(20))
                          ),
                          child: ClipRRect(
                            borderRadius: new BorderRadius.circular(10.0),
                            child: Image(
                              fit: BoxFit.fill,
                              width: ScreenUtil().setWidth(900),
                              height: 0.3*ScreenUtil().setWidth(900),
                              image: AssetImage('images/fl_vip.png'),
                            ),
                          )
                        ),
                      ),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: ScreenUtil().setHeight(100),left: ScreenUtil().setWidth(110)),
                    child: InkWell(
                      onTap: () async{
                        SharedPreferences shared = await SharedPreferences.getInstance();
                        if(shared.getString("login") == "true") {
                          ///开通会员
                          showDialog(
                            context: context,
                            builder: (BuildContext context) {
                              code = 'year';
                              return dialog;
                            },
                          );
                        }
                        else
                          {
                            print("先登录");
                            Navigator.push(context, MaterialPageRoute(
                                builder: (context) => LoginNavPage()
                            ));
                          }
                      },
                      child: InkWell(
                        child: Container(
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.all(Radius.circular(20))
                          ),
                          child: ClipRRect(
                            borderRadius: new BorderRadius.circular(10.0),
                            child: Image(
                              fit: BoxFit.fill,
                              width: ScreenUtil().setWidth(900),
                              height: 0.3*ScreenUtil().setWidth(900),
                              image: AssetImage('images/fl_vipYear.png'),
                            ),
                          )
                        ),
                      ),
                    ),
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  pay(String data) async{
    var result = await await tobias.aliPay(data);
    print('result:${result}');
  }
}
